<?php


namespace app\api\controller;

use CURLFile;

class Imgbed extends Base
{
    public $upload_max_size     = 5242880;
    public $upload_allowed_exts = 'jpg,png,gif,bmp';

    public function index($type = 'lmtc')
    {
        $path = 'upload/imgbed';
        $file = request()->file('file');

        $info = $file->validate(['size' => $this->upload_max_size, 'ext' => $this->upload_allowed_exts])
            ->move($path);

        if ($info) {
            $uploadFile = $info->getPathname();

            $rs = $this->nostorageUpload($type, $uploadFile);
            if ($rs['status']) {
                $this->success('上传成功！', '', ['src' => $rs['src']]);
            } else {
                $this->error('上传失败！可能是服务器接口问题。请重试！');
            }
        } else {
            // 上传失败获取错误信息
            $this->error('上传失败！Error：' . $file->getError());
        }
    }

    private function nostorageUpload($type, $uploadFile)
    {
        header("Content-type:application/json");
        $upload_url = [
            'bdtc' => ['url' => 'https://ufo.baidu.com/listen/api/upload', 'name' => 'file'],
            'xltc'  => ['url' => 'https://iask.sina.com.cn/question/ajax/fileupload', 'name' => 'wenwoImage'],
            'txtc'    => ['url' => 'https://om.qq.com/image/orginalupload', 'name' => 'Filedata'],
            'lmtc'  => ['url' => 'https://img.ybqsynet.cn/api/upload', 'name' => 'fileupload'],
        ];
        //初始化CURL，上传到远程服务器
        $ch = curl_init();
        //目标服务器地址
        curl_setopt($ch, CURLOPT_URL, $upload_url[$type]['url']);
        //设置上传的文件
        curl_setopt($ch, CURLOPT_POST, true);
        $data = array($upload_url[$type]['name'] => new CURLFile($uploadFile));
        curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        // 对认证证书来源的检查
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
        // 从证书中检查SSL加密算法是否存在
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
        //获取的信息以文件流的形式返回，而不是直接输出
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        $uploadRes = curl_exec($ch);
        //解析JSON
        $arr_result = json_decode($uploadRes, true);
        //关闭请求
        curl_close($ch);
        unlink($uploadFile);
        if ($type == 'bdtc') {
            if ($arr_result['error_no'] == '0') {
                return ['status' => 1, 'src' => $arr_result['data']];
            } else {
                return ['status' => 0];
            }
        } elseif ($type == 'xltc') {
            return ['status' => 1, 'src' => 'https://pic.iask.cn/fimg/' . $arr_result["id"] . '.jpg'];
        } elseif ($type == 'txtc') {
            if ($arr_result['response']['code'] == 0) {
                return ['status' => 1, 'src' => $arr_result['data']['url']];
            } else {
                return ['status' => 0];
            }
        } elseif ($type == 'lmtc') {
            if ($arr_result['success']) {
                return ['status' => 1, 'src' => $arr_result['url']];
            } else {
                return ['status' => 0];
            }
        }
    }
}